package com.jiaquyun.jcyx.jetpack.api

import com.blankj.utilcode.util.GsonUtils
import com.jiaquyun.jcyx.app.JCApplication
import com.jiaquyun.jcyx.base.BaseManager
import com.jiaquyun.jcyx.entity.*
import com.module.lemlin.http.HttpResponseData
import rxhttp.RxHttp
import rxhttp.await

class UserRepository : BaseManager() {

    /**
     * 发送验证码
     * */
    suspend fun smsSend(
        mobile: String, event: String
    ): SmsResponse = RxHttp.postForm("/api/sms/send")
        .add("mobile", mobile)
        .add("event", event)
        .await()


    /**
     * 登出
     * */
    suspend fun userLogout(): HttpResponseData =
        RxHttp.postForm("/api/user/logout")
            .await()

    /**
     * 微信登录
     * */
    suspend fun userThird(code: String): HttpResponseData =
        RxHttp.postForm("/api/user/third")
            .add("code", code)
            .await()

    /**
     * 手机号登录
     * */
    suspend fun userMobileLogin(
        mobile: String, captcha: String
    ): LoginResponse = RxHttp.postForm("/api/user/mobilelogin")
        .add("mobile", mobile)
        .add("captcha", captcha)
        .await()

    /**
     * 收藏列表
     * */
    suspend fun userCollectList(page: String, rows: String): CollectGoodsResponse =
        RxHttp.postForm("/api/user/collectList")
            .add("shop_id", "${JCApplication.userShops?.id ?: "55"}")
            .add("page", page)
            .add("rows", rows)
            .await()

    /**
     * 常购清单https://mall.jiaquyun.cn/api/goods/oftenGoods?page=1&rows=20&shop_id=55
     * */
    suspend fun userPurchaseList(
        page: String,
        rows: String
    ): PurchaseGoodsResponse =
        RxHttp.postForm("/api/goods/oftenGoods")
            .add("shop_id", "${JCApplication.userShops?.id ?: "55"}")
            .add("page", page)
            .add("rows", rows)
            .await()

    /**
     * 充值
     * */
    suspend fun userRecharge(
        money: String, payType: String
    ): HttpResponseData = RxHttp.postForm("/api/user/recharge")
        .add("money", money)
        .add("payType", payType)
        .add("shop_id", "${JCApplication.userShops?.id ?: "55"}")
        .await()

    /**
     * 充值列表
     * */
    suspend fun userRechargeList(): HttpResponseData =
        RxHttp.postForm("/api/user/recharge_list")
            .await()

    /**
     * 个人中心
     * */
    suspend fun userIndex(): UserIndexResponse =
        RxHttp.get("/api/user/index")
            .await()

    /**
     * 足迹
     * */
    suspend fun userVisitLog(): HttpResponseData =
        RxHttp.get("/api/user/visitLog")
            .add("shop_id", "${JCApplication.userShops?.id ?: "55"}")
            .await()

    /**
     * 积分签到
     * */
    suspend fun userScoreSign(): HttpResponseData =
        RxHttp.get("/api/user/scoreSign")
            .await()

    /**
     * 积分记录
     * */
    suspend fun userScoreLog(
        type: String,
        page: String,
        rows: String
    ): UserScoreResponse = RxHttp.get("/api/user/scoreLog")
        .add("type", type)
        .add("page", page)
        .add("rows", rows)
        .await()

    /**
     * 积分签到记录
     * */
    suspend fun userScoreSignList(): UserSignListResponse =
        RxHttp.get("/api/user/scoreSignList")
            .await()

    /**
     * 收货地址列表
     * */
    suspend fun userAddressList(): AddressListResponse =
        RxHttp.postForm("/api/user/addressList")
            .await()

    /**
     * 保存收货地址
     * */
    suspend fun userAddressSave(save: AddressSave): HttpResponseData {
        val maps: Map<String, String?> =
            GsonUtils.fromJson<Map<String, String?>>(
                GsonUtils.toJson(save),
                Map::class.java
            ).filter { it.value != null }
        return RxHttp.postForm("/api/user/saveAddress")
            .addAll(maps)
            .await()
    }

    /**
     * 收货地址详情
     *
     * @param id 地址id
     * */
    suspend fun userAddressDetail(id: String): AddressDetailsResponse =
        RxHttp.postForm("/api/user/addressDetail")
            .add("id", id)
            .await()

    /**
     * 收货地址删除
     *
     * @param id 地址id
     * */
    suspend fun userAddressDelete(id: String): HttpResponseData =
        RxHttp.postForm("/api/user/delAddress")
            .add("id", id)
            .await()

    /**
     * 门店地址列表https://mall.jiaquyun.cn/api/shops/shopList
     *
     * @param id 地址id
     * */
    suspend fun userShopsList(lng: String, lat: String): UserShopsResponse =
        RxHttp.postForm("/api/shops/shopList")
            .add("lng", lng)
            .add("lat", lat)
            .await()


    /**
     * https://mall.jiaquyun.cn/api/coupon/userCouponList
     * */
    suspend fun userCouponList(
        status: String,
        page: String,
        rows: String
    ): CouponListResponse =
        RxHttp.postForm("/api/coupon/userCouponList")
            .add("status", status)
            .add("page", page)
            .add("rows", rows)
            .await()

    //https://mall.jiaquyun.cn/api/user/moneylog?page=1&rows=10
    suspend fun userMoneyLog(
        page: String, rows: String
    ): UserMoneyLogResponse = RxHttp.postForm("/api/user/moneylog")
        .add("page", page)
        .add("rows", rows)
        .await()

    //
    suspend fun userYongApply(money: String): HttpResponseData =
        RxHttp.postForm("/api/user_commission/applyCommissionWithdraw")
            .add("money", money)
            .await()

    //https://mall.jiaquyun.cn/api/user_commission/commissionList?page=1&rows=10
    suspend fun userYongLog(
        page: String, rows: String
    ): UserYongLogResponse = RxHttp.postForm("/api/user_commission/commissionList")
        .add("page", page)
        .add("rows", rows)
        .await()

    //https://mall.jiaquyun.cn/api/order_invoice/index
    suspend fun userInvoiceHistory(
        page: String, rows: String
    ): UserInvoiceHistoryResponse = RxHttp.postForm("/api/order_invoice/index")
        .add("page", page)
        .add("rows", rows)
        .await()

    //https://mall.jiaquyun.cn/api/order_invoice/orderInvoiceList
    suspend fun userInvoiceOrder(
        page: String, rows: String
    ): UserInvoiceOrderResponse = RxHttp.postForm("/api/order_invoice/orderInvoiceList")
        .add("shop_id", "${JCApplication.userShops?.id ?: "55"}")
        .add("page", page)
        .add("rows", rows)
        .await()

    //https://mall.jiaquyun.cn/api/order_invoice/createInvoice
    suspend fun userInvoiceCreate(
        create: UserInvoiceCreate
    ): HttpResponseData = RxHttp.postBody("/api/order_invoice/createInvoice")
        .setJsonBody(create)
        .await()

    //https://mall.jiaquyun.cn/api/order_invoice/queryInvoice
    suspend fun userInvoiceQuery(invoice_no: String): UserInvoiceQueryResponse =
        RxHttp.postForm("/api/order_invoice/queryInvoice")
            .add("invoice_no", invoice_no)
            .await()
}